package demo.mq.utils;

import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 *
 * @author 雷霆
 * @version 1.0.0
 * @date$ 2018/8/3
 *
 * Description：
 *
 * Modification History:
 *
 */
public class StringUtil {

    public static char SEPARATOR_COMMA=',';
    public static String M15="***************";
    public static String M7="*******";
    public static final String REGEX1=":=";
    public static final String FILE_SUFFIX=".txt";
    public static final String REGEX2="-";
    public static final String UNDERLINE="_";



    public static boolean isMobileNO(String mobiles) {
        String telRegex = "[1][3456789]\\d{9}";
        // "[1]"代表第1位为数字1，"[3578]"代表第二位可以为3、5、8中的一个，"\\d{9}"代表后面是可以是0～9的数字，有9位。
        if (StringUtils.isEmpty(mobiles)) {
            return false;
        } else
            return mobiles.matches(telRegex);
    }


    public static String listToString(List list, char separator) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < list.size(); i++) {
            sb.append(list.get(i)).append(separator);
        }
        return sb.toString().substring(0, sb.toString().length() - 1);
    }

    /**
     * 使得字符串首字母小写
     * @return
     */
     public static String lowerFirst(String str){

         if(StringUtils.isEmpty(str)){
             return str;
         }

         char [] chars=str.toCharArray();
         chars[0]+=32;
         return String.valueOf(chars);

     }

     public static String getPrefixFromEncStr(String encStr){
         if(StringUtils.isEmpty(encStr)){
             return"";
         }
         if(encStr.contains(REGEX1)){
            String [] strArr=encStr.split(REGEX1);
            return strArr[0];
         }

         return encStr;
     }

    public static String  getCretNoTicket(String cretNoEnc,String cretNo){
        return  cretNoEnc+REGEX1+getTicket(cretNo,M15);
    }

    public static String  getMobileTicket(String cretNoEnc,String cretNo){
        return  cretNoEnc+REGEX1+getTicket(cretNo,M7);
    }

    public static String getTicket(String cretNo,String summery){

        if(StringUtils.isEmpty(cretNo)){
            return "";
        }

        String startStr=cretNo.substring(0,1);
        String endStr=cretNo.substring(cretNo.length()-1);
        StringBuilder result=new StringBuilder(startStr+summery);

        if(endStr.equals("X")){
            endStr=cretNo.substring(cretNo.length()-2);
        }else{
            result.append("*");
        }

        result.append(endStr);
        return result.toString();
    }




    public static String setNodeText(String xmlString, String nodeName,String newNodeValue) throws Exception {
        String beginName = "<" + nodeName + ">";
        String endName = "</" + nodeName + ">";
        int beginIndex = xmlString.indexOf(beginName);
        if(beginIndex == -1) {
            return xmlString;
        } else {
            int endIndex = xmlString.indexOf(endName);
            if(endIndex == -1) {
                return xmlString;
            } else {
                String oldValue = xmlString.substring(beginIndex + beginName.length(), endIndex);
                oldValue=beginName+oldValue+endName;
                newNodeValue=beginName+newNodeValue+endName;
                String[] strings= xmlString.split(oldValue);
                return strings[0]+newNodeValue+strings[1];
            }
        }
    }


    public static String getNodeText(String xmlString, String nodeName) {
        String beginName = "<" + nodeName + ">";
        String endName = "</" + nodeName + ">";
        int beginIndex = xmlString.indexOf(beginName);
        if(beginIndex == -1) {
            return "";
        } else {
            int endIndex = xmlString.indexOf(endName);
            if(endIndex == -1) {
                return "";
            } else {
                String nodeValue = xmlString.substring(beginIndex + beginName.length(), endIndex);
                return nodeValue;
            }
        }
    }






}
